package com.piece.admin.controller.monitor;

import com.piece.admin.controller.ControllerSupport;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.constant.FrameWorkConstants;
import com.piece.core.mongo.service.MongoStreamBlockService;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.support.server.Server;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@Api(tags = {"服务器系统资源管理"})
@RequestMapping("/api/monitor")
public class MonitorController extends ControllerSupport {

    @Resource
    private MongoStreamBlockService mongoStreamBlockService;

    @GetMapping("/server_info")
    @AclPermission(code = "monitor:server:view")
    @ApiOperation(value = "服务器资源信息")
    public AjaxResponse serverInfo() {
        try {
            Server server = new Server();
            server.copyTo();
            return success(I18nUtil.message(ExceptionConstants.MESSAGE_SUCCESS), server);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SERVER), e.getMessage());
        }
    }

    @PostMapping("/block/table")
    @AclPermission(code = "monitor:block:view")
    @ApiOperation(value = "查询黑名单")
    @ApiImplicitParam(value = "黑名单列表查询条件", dataTypeClass = QueryTable.class ,type = "body")
    public AjaxResponse blackTable() {
        QueryTable queryTable = initTable();
        DataTable dataTable = mongoStreamBlockService.findPage(queryTable);
        return success(dataTable);
    }

    @SubmitPermission
    @PostMapping("/block/delete")
    @AclPermission(code = "monitor:block:delete")
    @ApiOperation(value = "删除黑名单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键集合"),
            @ApiImplicitParam(name = "ip", value = "ip地址")
    })
    public AjaxResponse deleteBlack() {
        try {
            String ids = getRequest().getParameter("ids");
            String ip = getRequest().getParameter("ip");
            if (StringUtil.isNotEmpty(ids)) {
                String[] idArr = ids.split(FrameWorkConstants.SPLIT_PARAM);
                for (int i = 0; i < idArr.length; i++) {
                    mongoStreamBlockService.deleteByIp(idArr[i]);
                }
            } else if (StringUtil.isNotEmpty(ip)) {
                String[] ipArr = ip.split(FrameWorkConstants.SPLIT_PARAM);
                for (int i = 0; i < ipArr.length; i++) {
                    mongoStreamBlockService.deleteByIp(ipArr[i]);
                }
            } else {
                //清空所有数据
                mongoStreamBlockService.clear();
            }
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.DELETE_FAILURE), e.getMessage());
        }

        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }
}
